package dao

import "encoding/json"

type Organization struct {
	BaseModel
	Code     string `json:"code"`
	Name     string `json:"name"`
	ParentId int    `json:"parent_id"`
	Path     string `json:"path"`
	Extra    string `json:"extra"`
}

func (c *Organization) TableName() string {
	return "organization"
}

func (c *Organization) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func SelectAllOrganization() *[]Organization {
	organization := make([]Organization, 0)
	DB.Where("is_del = ?", 0).Find(&organization)
	return &organization
}

func SelectOrganizationByExample(organization *Organization) *[]Organization {
	organizations := make([]Organization, 0)
	DB.Where(&organization)
	DB.Find(&organizations)
	return &organizations
}

func SaveOrganization(organization *Organization) int {
	DB.Create(&organization)
	return organization.ID
}

func SaveOrganizationBatch(organizations *[]Organization) {
	for _, organization := range *organizations {
		DB.Create(&organization)
	}
}

func UpdateOrganization(organization *Organization) {
	DB.Model(&organization).Save(&organization)
}

func DeleteOrganization(id int) {
	organization := Organization{}
	organization.ID = id
	DB.Delete(organization)
}

func DeleteOrganizationByExample(organization *Organization) {
	DB.Where(&organization).Delete(Organization{})
}
